#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>

using namespace std;

template <class T>
class Stack {
    private:
        vector<T> elms; //元素

    public:
        void push(T const&); //入栈
        void pop();  //出栈
        T top() const; //返回栈顶元素
        bool empty() const{
            return elms.empty();
        }
};

template <class T>
void Stack<T>::push(T const& elem){
    elms.push_back(elem);
}

template <class T>
void Stack<T>::pop () {
    if(elms.empty()){
        throw out_of_range("Stack<>:pop():empty stack");
    }
    elms.pop_back(); //删除最后一个元素
}

template <class T>
T Stack<T>::top () const 
{
    if (elms.empty()){
        throw out_of_range("Stack<>::top():empty stack");
    }
    return elms.back();
}

int main() {
    try
    {
        Stack<int> intStack; //int类型栈
        Stack<string> stringStack; //string类型栈

        //操作int类型栈
        intStack.push(7);
        cout << intStack.top() << endl;

        //操作string类型栈
        stringStack.push("hello");
        cout << stringStack.top() << endl;
        stringStack.pop();
        stringStack.pop();
    }
    catch(exception const& ex)
    {
        cerr << "Exception:" << ex.what() << endl;
        return -1;
    }
    
}